<template>
  <div class="log">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        :permission="permissionList"
        :search.sync="searchForm"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @search-reset="searchReset"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-del="handleDel">
        <template slot-scope="scope" slot="createTimeSearch">
          <el-date-picker
            v-model="searchForm.createTime"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间">
          </el-date-picker>
        </template>
        <template slot-scope="scope" slot="menuLeft">
          <el-button type="success" icon="el-icon-delete" @click="deleteVisible = true">清空日志</el-button>
        </template>
      </avue-crud>

    </basic-container>
    <el-dialog title="选择要清空的时间段" :visible.sync="deleteVisible" width="470px">
      <div>
        时间段：
        <el-date-picker
          v-model="deleteDate"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </div>
      <span slot="footer" class="dialog-footer">
      <el-button @click="deleteVisible = false">取 消</el-button>
      <el-button type="primary" @click="deleteLog" :loading="clear.loading">{{clear.text}}</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import {delObj, fetchList, delTime} from '@/api/admin/log'
  import {tableOption} from '@/const/crud/admin/log'
  import {mapGetters} from 'vuex'

  export default {
    name: 'Log',
    data() {
      return {
        tableData: [],
        searchForm: {},
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption: tableOption,

        deleteDate:[],
        deleteVisible: false,
        clear:{
          loading: false,
          text:'确定',
          defaultText: '确定',
          loadingText: '正在清空'
        }
      }
    },
    computed: {
      ...mapGetters(['permissions']),
      permissionList() {
        return {
          delBtn: this.vaildData(this.permissions.sys_log_del, false)
        }
      }
    },
    methods: {
      getList(page, params) {
        this.tableLoading = true
        fetchList(Object.assign({
          descs: 'create_time',
          current: page.currentPage,
          size: page.pageSize
        }, params, this.searchForm)).then(response => {
          this.tableData = response.data.data.records
          this.page.total = response.data.data.total
          this.tableLoading = false
        })
      },
      handleDel: function (row) {
        this.$confirm('是否确认删除该条日志?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(row.id)
        }).then(() => {
          this.getList(this.page)
          this.$message.success('删除成功')
        })
      },
      searchReset() {
        this.searchForm = {}
        this.page.currentPage = 1
        this.getList(this.page)
      },
      searchChange(form, done) {
        this.searchForm = Object.assign(this.searchForm,form)
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      currentChange(current) {
        this.page.currentPage = current
      },
      refreshChange() {
        this.getList(this.page)
      },

      deleteLog(){
        if (!this.deleteDate.length){
          this.$message.error('请选择时间段')
          return false
        }
        this.loadingClear()

        let params = {
          startTime: this.deleteDate[0]+ ' 00:00:00',
          endTime: this.deleteDate[1]+ ' 23:59:59'
        }
        delTime(params).then(res => {
          this.loadedClear()
          this.deleteVisible = false
          this.refreshChange()
          this.$message.success(`${this.deleteDate[0]} 到 ${this.deleteDate[1]} 时间段内日志清空成功`)
        })

        // setTimeout(() => {
        //   this.loadedClear()
        //   this.deleteVisible = false
        //   this.$message.success(`${this.deleteDate[0]} 到 ${this.deleteDate[1]} 时间段内日志清空成功`)
        // },1000)
      },
      loadingClear(){
        this.clear.text = this.clear.loadingText
        this.clear.loading = true
      },
      loadedClear(){
        this.clear.text = this.clear.defaultText
        this.clear.loading = false
      },
    }
  }
</script>
